<?php
/**
 * Created by PhpStorm.
 * User: hansliao
 * Date: 2017/9/10
 * Time: 22:33
 */

namespace Admin\Controller;

class OrderController extends AdminController{

    public function index($nickname = '',$otid='',$status=0,$p=0){
        $status = intval($status);
        $otid = htmlspecialchars($otid);
        $nickname = htmlspecialchars($nickname);

        $page = intval($p);
        $page = $page ? $page : 1;
        $row = 15;

        $where = array();
        if($status>0){
            $where['status'] = $status;
        }
        if($otid){
            $where['out_trade_no'] = array('like',"%$otid%");
        }
        if($nickname){
            $where['nickname'] = array('like',"%$nickname%");
        }



        $count = M('order')->where($where)->count();
        $orders = M('order')->where($where)->order('update_time')->page($page,$row)->select();

        $page = new \Think\Page($count,$row);
        $page->setConfig('theme','%FIRST% %UP_PAGE% %LINK_PAGE% %DOWN_PAGE% %END% %HEADER%');
        $this->assign('status',$status);
        $this->assign('_page', $page->show());
        $this->assign('orders',$orders);
        $this->display();
    }

    public function pregooods(){
        $pc = C('precategory');
        $pgs = M('goods')->where("category = $pc")->getField('id,spec',true);

        $where = array();
        $tt = array();
        foreach($pgs as $k=>$v){
            $tt[] = $k;
        }

        $where['goods_id'] = array('in',$tt);
        $where['create_time'] = array('EGT',date("Y-m-d 00:00:00",time()));
        $pogs = M('order_goods')->where($where)->select();
        $oids= array();
        foreach($pogs as $v){
            $oids[] = $v['oid'];
        }
        $where = array();
        $where['id'] = array('in',$oids);
        $where['status'] = array('gt',1);
        $poids = M('order')->where($where)->getField('id,out_trade_no,nickname');
        $oids= array();
        foreach($poids as $v){
            $oids[] = $v['id'];
        }
        foreach($pogs as $k=>$v){
            if(!in_array($v['oid'],$oids)){
                unset($pogs[$k]);
            }else{
                $pogs[$k]['out_trade_no'] = $poids[$v['oid']]['out_trade_no'];
                $pogs[$k]['nickname'] = $poids[$v['oid']]['nickname'];
                $pogs[$k]['spec'] = $pgs[$v['goods_id']];
            }
        }

        $this->assign('pogs',$pogs);
        $this->display();
    }
    public function finshorder($id){
        if(M('order')->where("id = $id and status = 2")->find()){
            $data = array();
            $data['status'] = 4;
            $data['update_time'] = time();
            if(M('order')->where("id = $id and status = 2")->save($data)){
                $this->success('操作成功！');
            }else{
                $this->success('操作失败！');
            }
        }

    }
}